using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


class SetActiveNode : TutorialEventNode
{
    [JsonAttribute("uiPath", "需要设置的UI路径")]
    public List<string> uiPath = new List<string>() { "" };

#if UNITY_EDITOR
    [JsonEditorAttribute(typeof(UIPathSwitchMember), "uiPath")]
    public List<object> selectUI = new List<object>();
#endif

    [JsonAttribute("actv", "是否隐藏")]
    public bool active = false;

    [JsonAttribute("reset", "结束之后是否重置")]
    public bool resetOnEnd = false;

    GameObject chooseUI;
     
    protected override void Cancel()
    {
        if (resetOnEnd)
            SetCanShow(this.active);
    }

    /// <summary>
    /// 初始化
    /// </summary>
    protected override void Init()
    {
        string dir; string file;
        UITools.GetResourcePath(uiPath[0], out dir, out file);
        Hope.UIManager.Instance.ShowPanel(file, dir, this.OnOpenUI);
    }


    /// <summary>
    /// 打开UI之后处理
    /// </summary>
    /// <param name="obj"></param>
    protected void OnOpenUI(GameObject obj)
    {
        this.chooseUI = obj;
        this.SetCanShow(!this.active);
    }

    /// <summary>
    /// 打开UI之后处理
    /// </summary>
    /// <param name="obj"></param>
	protected virtual void SetCanShow(bool bShow)
    {
        Transform root = this.chooseUI.transform;
        for (int i = 1; i < this.uiPath.Count; i++)
        {
            var child = root.Find(string.Format(UITools.GetTutorialChildPath(uiPath[i])));
            if (child == null)
            {
                Debug.LogErrorFormat("child path is Error, please Check Node, parentPath is : {0}, ChildPath is {1}", this.uiPath[0], this.uiPath[i]);
                continue;
            }
            int layer = bShow ? LayerMask.NameToLayer(GameConst.UI_Layer) : LayerMask.NameToLayer(GameConst.Hide_Layer);
            child.SetLayer(layer);
            child.gameObject.SetActive(bShow);
        }
    }
     
}
